package com.koushikdutta.async.http.filter;

import com.getpebble.android.R;
import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.FilteredDataEmitter;
import com.koushikdutta.async.Util;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;

public class ChunkedInputFilter extends FilteredDataEmitter {
    static final /* synthetic */ boolean $assertionsDisabled = (!ChunkedInputFilter.class.desiredAssertionStatus());
    private int mChunkLength = 0;
    private int mChunkLengthRemaining = 0;
    private State mState = State.CHUNK_LEN;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$koushikdutta$async$http$filter$ChunkedInputFilter$State = new int[State.values().length];

        static {
            try {
                $SwitchMap$com$koushikdutta$async$http$filter$ChunkedInputFilter$State[State.CHUNK_LEN.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$koushikdutta$async$http$filter$ChunkedInputFilter$State[State.CHUNK_LEN_CR.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$koushikdutta$async$http$filter$ChunkedInputFilter$State[State.CHUNK.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$koushikdutta$async$http$filter$ChunkedInputFilter$State[State.CHUNK_CR.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$koushikdutta$async$http$filter$ChunkedInputFilter$State[State.CHUNK_CRLF.ordinal()] = 5;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$com$koushikdutta$async$http$filter$ChunkedInputFilter$State[State.COMPLETE.ordinal()] = 6;
            } catch (NoSuchFieldError e6) {
            }
        }
    }

    private enum State {
        CHUNK_LEN,
        CHUNK_LEN_CR,
        CHUNK_LEN_CRLF,
        CHUNK,
        CHUNK_CR,
        CHUNK_CRLF,
        COMPLETE
    }

    private boolean checkByte(char b, char value) {
        if (b == value) {
            return true;
        }
        report(new ChunkedDataException(value + " was expected, got " + b));
        return false;
    }

    private boolean checkLF(char b) {
        return checkByte(b, '\n');
    }

    private boolean checkCR(char b) {
        return checkByte(b, '\r');
    }

    protected void report(Exception e) {
        if (e == null && this.mState != State.COMPLETE) {
            e = new ChunkedDataException("chunked input ended before final chunk");
        }
        super.report(e);
    }

    public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
        while (bb.remaining() > 0) {
            try {
                switch (AnonymousClass1.$SwitchMap$com$koushikdutta$async$http$filter$ChunkedInputFilter$State[this.mState.ordinal()]) {
                    case SMTPMessage.RETURN_FULL /*1*/:
                        char c = bb.getByteChar();
                        if (c == '\r') {
                            this.mState = State.CHUNK_LEN_CR;
                        } else {
                            this.mChunkLength *= 16;
                            if (c >= 'a' && c <= 'f') {
                                this.mChunkLength += (c - 97) + 10;
                            } else if (c >= '0' && c <= '9') {
                                this.mChunkLength += c - 48;
                            } else if (c < 'A' || c > 'F') {
                                report(new ChunkedDataException("invalid chunk length: " + c));
                                return;
                            } else {
                                this.mChunkLength += (c - 65) + 10;
                            }
                        }
                        this.mChunkLengthRemaining = this.mChunkLength;
                        break;
                    case SMTPMessage.RETURN_HDRS /*2*/:
                        if (checkLF(bb.getByteChar())) {
                            this.mState = State.CHUNK;
                            break;
                        }
                        return;
                    case ListInfo.INDETERMINATE /*3*/:
                        int remaining = bb.remaining();
                        int reading = Math.min(this.mChunkLengthRemaining, remaining);
                        this.mChunkLengthRemaining -= reading;
                        if (this.mChunkLengthRemaining == 0) {
                            this.mState = State.CHUNK_CR;
                        }
                        if (reading != 0) {
                            ByteBufferList chunk = bb.get(reading);
                            int newRemaining = bb.remaining();
                            if (!$assertionsDisabled && remaining != chunk.remaining() + bb.remaining()) {
                                throw new AssertionError();
                            } else if ($assertionsDisabled || reading == chunk.remaining()) {
                                Util.emitAllData(this, chunk);
                                if (!($assertionsDisabled || newRemaining == bb.remaining())) {
                                    throw new AssertionError();
                                }
                            } else {
                                throw new AssertionError();
                            }
                        }
                        continue;
                        break;
                    case SMTPMessage.NOTIFY_DELAY /*4*/:
                        if (checkCR(bb.getByteChar())) {
                            this.mState = State.CHUNK_CRLF;
                            break;
                        }
                        return;
                    case R.styleable.WalletFragmentStyle_maskedWalletDetailsHeaderTextAppearance /*5*/:
                        if (checkLF(bb.getByteChar())) {
                            if (this.mChunkLength > 0) {
                                this.mState = State.CHUNK_LEN;
                            } else {
                                this.mState = State.COMPLETE;
                                report(null);
                            }
                            this.mChunkLength = 0;
                            break;
                        }
                        return;
                    case R.styleable.WalletFragmentStyle_maskedWalletDetailsBackground /*6*/:
                        if (!$assertionsDisabled) {
                            throw new AssertionError();
                        }
                        return;
                    default:
                        break;
                }
            } catch (Exception ex) {
                report(ex);
                return;
            }
        }
    }
}
